﻿using System;
using System.Collections.Generic;
using System.Linq;
using DCS.Business.Model;
using DCS.DataProvider;
using CarolLib;
using System.Configuration;

namespace DCS.Business.Manager
{
    public class LDMMessageManager
    {
        
        private IDataProvider provider;
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public LDMMessageManager(IDataProvider p)
        {
            this.provider = p;
        }

        public void SaveEntities(List<LDMMessage> results)
        {
            foreach (var item in results)
            {
                try
                {
                    provider.CreateLDMMessage(item);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                }
            }
            
        }

        public void SaveDistinctEntities(List<LDMMessage> results)
        {
            foreach (var item in results)
            {
                try
                {
                    provider.CreateDistinctLDMMessage(item);
                }
                catch (Exception e)
                {
                    Logger.Error(e);
                }
            }

        }

        #region Publish

        private static readonly string KEY_GetLDM = "BY-FQ-{0}{1}-{2}-{3}{4}";
        protected int CacheSeconds
        {
            get
            {
                if (ConfigurationManager.AppSettings["CacheSeconds"] != string.Empty)
                {
                    return int.Parse(ConfigurationManager.AppSettings["CacheSeconds"]);
                }
                else
                {
                    return 1;
                }
            }
        }

        public List<LDMMessage> GetLDMMessage(string carrier, string flightNo, DateTime pdate, string dept, string arr)
        {
            List<LDMMessage> result = null;
            string sql = string.Empty;
            if (!CacheHelper.TryGet<List<LDMMessage>>(GetCacheKey(carrier, flightNo, pdate, dept, arr), out result))
            {
                try
                {
                    if (pdate <= DateTime.Today.AddDays(-7))
                    {
                        sql = string.Format(ConfigurationManager.AppSettings["SQL_GetLDMHis"], carrier, flightNo, pdate.ToShortDateString(), dept, arr);
                    }
                    else
                    {
                        sql = string.Format(ConfigurationManager.AppSettings["SQL_GetLDM"], carrier, flightNo, pdate.ToShortDateString(), dept, arr);
                    }
                    result = provider.QueryModelList<LDMMessage>(sql);
                    CacheHelper.Insert(GetCacheKey(carrier, flightNo, pdate, dept, arr), result, CacheSeconds);
                }
                catch (Exception e)
                {
                    Logger.Error("Error During GetLDMMessage", e);
                }
            }
            return result;
        }

        private static string GetCacheKey(string carrier, string flightNo, DateTime pdate, string dept, string arr)
        {
            return string.Format(KEY_GetLDM, carrier, flightNo, pdate.ToShortDateString(), dept, arr);
        }

        #endregion

        #region FIDS LDM Parse

        public List<FIDSLDMMsg> GetNewLDMMsg(decimal MaxID)
        {
            return provider.GetNewLDMMsg(MaxID);
        }

        public List<FlightInfo> GetFlightInfoByPara(DateTime PDate, string Carrier, string FlightNo)
        {
            return provider.GetFlightInfoByPara(PDate, Carrier, FlightNo);
        }

        #endregion
    }
}
